package linearList;

public class SeqList<T> implements List<T> {
    private Object[] elements;
    private int length;

    public SeqList() {
        this.elements = new Object[8];
        this.length = 0;
    }

    public SeqList(T[] data) {
        this.elements = new Object[data.length];
        for(int i = 0; i < data.length; i++) {
            this.elements[i] = data[i];
        }

        this.length = data.length;
    }

    public static void main(String[] args) {
        System.out.println("--测试初始化--");
        Integer[] data = {66, 33, 15, 11, 78, 4, 6, 9, 6, 10};
        SeqList<Integer> seqList = new SeqList<Integer>(data);
        System.out.println(seqList);

        System.out.println("--测试insert(2, 22)--");
        seqList.insert(2, 22);
        System.out.println(seqList);

        System.out.println("--测试insert 10个元素--");
        for (int i = 20; i <= 30; i++) {
            seqList.insert(i);
        }
        System.out.println(seqList);

        System.out.println("--测试remove index为10的元素--");
        seqList.removeByKey(10);
        System.out.println(seqList);

        System.out.println("--测试获取 index为5的元素--");
        System.out.println(seqList.get(5));

        System.out.println("--测试设置 index为5的元素为100--");
        seqList.set(5, 100);
        System.out.println(seqList);

        System.out.println("--测试查找元素等于100的索引--");
        System.out.println(seqList.search(100));

        System.out.println("--测试remove by key--");
        seqList.removeByKey(new Integer(100));
        System.out.println(seqList);

    }

    @Override
    public boolean isEmpty() {
        if(this.length==0)
            return true;
        else {
            return false;
        }
    }

    @Override
    public int size() {
        return this.length;
    }

    @Override
    public T get(int i) {
        if (i<this.length && i>=0)
            return (T) this.elements[i];
        return null;
    }

    @Override
    public void set(int i, T x) {
        if(x != null){
            this.elements[0] = x ;
        }
    }

    @Override
    public int insert(int i, T x) {
        if(x == null) {
            return -1;
        }
        if(i < 0) {
            i =  0;
        }
        if (i>this.length) {
            i=this.length;
        }
        return 0;
    }

    @Override
    public int insert(T x) {
        return this.insert(this.length,x);
    }

    @Override
    public T remove(int i) {
        if(i>=0 && i<this.length){
            T x = (T)this.elements[i];
            for(int j=1;j<this.length-1;j++)
                this.elements[j] = this.elements[j+1];
            this.elements[this.length-1]=null;
            this.length--;
            return x;
        }
        return null;
    }

    @Override
    public void clear() {
        this.length = 0;
    }

    @Override
    public int search(T key) {
        for(int i=0 ; i<this.length ; i++ ) {
            if(key.equals(this.elements[i])) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public T removeByKey(T key) {
        for (int i= key;i<length;i++)
        elements[key]=elements[key+1];
        length--;
    }
}